package cn.johnyu.demo.dao.impl;

import cn.johnyu.demo.dao.BookDao;
import cn.johnyu.demo.pojo.Book;
import cn.johnyu.demo.util.Dbutil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl implements BookDao {
    @Override
    public List<Book> findAll() {
        List<Book> books=new ArrayList<>();
        String sql="select * from books";
        Connection connection=null;
        connection= Dbutil.getConnection();
        PreparedStatement statement=null;
        ResultSet rs=null;

        try {
            statement=connection.prepareStatement(sql);
            rs=statement.executeQuery();
            while (rs.next()){

                Book book=new Book();
                book.setId(rs.getInt("id"));
                book.setName(rs.getString("name"));
                book.setPrice(rs.getFloat("price"));
                book.setPublishDate(rs.getDate("publish_date"));
                  books.add(book);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.close(connection,statement,rs);
        }
        return books;
    }

    @Override
    public Book findById(int id) {
        return null;
    }

    @Override
    public void save(Book book) {

    }
}
